package com.zhonghui.modlules.shop.eshopService.Ipml;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eshop.service.Impl.BaseServiceImpl;
import com.zhonghui.common.eshopDozer.config.IGenerator;
import com.zhonghui.common.eshopMapper.SystemConfigMapper;
import com.zhonghui.common.eshopUtil.RedisUtils;
import com.zhonghui.modlules.shop.eshopDomain.SystemConfig;
import com.zhonghui.modlules.shop.eshopService.SystemConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(
        propagation = Propagation.SUPPORTS,
        readOnly = true,
        rollbackFor = {Exception.class}
)
public class SystemConfigServiceImpl extends BaseServiceImpl<SystemConfigMapper, SystemConfig> implements SystemConfigService {
    private final RedisUtils redisUtils;
    private final IGenerator generator;

    public SystemConfigServiceImpl(final IGenerator generator, final RedisUtils redisUtils) {
        this.generator = generator;
        this.redisUtils = redisUtils;
    }

    public String getData(String name) {
        String result = this.redisUtils.getY(name);
        if (StrUtil.isNotBlank(result)) {
            return result;
        } else {
            LambdaQueryWrapper<SystemConfig> wrapper = new LambdaQueryWrapper();
            wrapper.eq(SystemConfig::getMenuName, name);
            SystemConfig systemConfig = (SystemConfig)((SystemConfigMapper)this.baseMapper).
                    selectOne(wrapper);
            return systemConfig == null ? "" : systemConfig.getValue();
        }
    }
}
